//
//  AddEduViewController.m
//  HubeiHotel
//
//  Created by LiBenCan on 17/11/22.
//  Copyright © 2017年 张露杰. All rights reserved.
//

#import "AddEduViewController.h"

#import "RWBChouseView.h"
#import "UILabel+HWDUILabel.h"
#import "EditCell.h"
#import "SelfTextView.h"
#import "RWBChouseTimeView.h"
#import "HopeCell.h"
@interface AddEduViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *tableV;
@property (nonatomic,strong)NSArray *BaseTitleArr;//标题

@property (nonatomic, copy) NSString *schoolName;
@property (nonatomic, copy) NSString *major;
@property (nonatomic, copy) NSString *graduationTime;
@property (nonatomic, copy) NSString *education;

@end

@implementation AddEduViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initTitleWithTitle:@"教育经历"];
    [self initRightBarButtonWithTitle:@"保存" andTitleColor:[UIColor whiteColor] andFont:16*screenW_6];
    _BaseTitleArr = @[@"学校名称",@"所学专业",@"毕业年份",@"学      历"];
    
    if (_edu) {
        _schoolName = _edu[@"schoolName"];
        _major = _edu[@"major"];
        _graduationTime = _edu[@"graduationTime"];
        _education = _edu[@"education"];
    }
    
    [self.view addSubview:self.tableV];
}
-(UITableView *)tableV//创建View
{
    if (!_tableV) {
        _tableV = [[UITableView alloc] initWithFrame:CGRectMake(0,0, SCREENW, SCREENH-64) style:UITableViewStyleGrouped];
        _tableV.estimatedRowHeight = 150*screenW_6;
        _tableV.backgroundColor = backColor;
        _tableV.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableV.showsVerticalScrollIndicator = NO;
        _tableV.delegate = self;
        _tableV.dataSource = self;
    }
    return _tableV;
}
#pragma Mark TableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _BaseTitleArr.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.000001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.000001;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    EditCell *cell = [tableView dequeueReusableCellWithIdentifier:@"EditCell"];
    if (!cell) {
        cell = [[EditCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"EditCell"];
    }
    NSString *str = @"输入";
    if (indexPath.row == 2 || indexPath.row == 3) {
        str = @"请选择";
    }
    NSString *text;
    if (indexPath.row == 0) {
        text = _schoolName;
    }else if (indexPath.row == 1){
        text = _major;
    }else if (indexPath.row == 2){
        text = _graduationTime;
    }else if (indexPath.row == 3){
        if (_education.length > 0) {
            switch ([_education integerValue]) {
                case 0:
                    text = @"大专";
                    break;
                case 1:
                    text = @"本科";
                    break;
                case 2:
                    text = @"硕士";
                    break;
                case 3:
                    text = @"博士";
                    break;
                case 4:
                    text = @"其他";
                    break;
                default:
                    break;
            }
        }
    }
    [cell loadCell:_BaseTitleArr[indexPath.row] text:text placeHodel:[NSString stringWithFormat:@"%@%@",str,_BaseTitleArr[indexPath.row]]];
    if (indexPath.row == 2 || indexPath.row == 3) {
        cell.JianTouView.hidden = NO;
        cell.textF.inputTf.enabled = NO;
    }
    __weak typeof(self) weakSelf = self;
    cell.blcok = ^(NSString *text){
        if (indexPath.row == 0) {
            weakSelf.schoolName = text;
        }else if(indexPath.row == 1){
            weakSelf.major = text;
        }
    };
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    __weak typeof(self) weakSelf = self;
    [self.view endEditing:YES];
    if (indexPath.row == 2) {
        RWBChouseTimeView *view = [[RWBChouseTimeView alloc]initWithDatePickerMode:UIDatePickerModeDate maxDate:nil minDate:nil Complete:^(NSDate *date) {
            weakSelf.graduationTime = [TomamaUI dateTostiring:date andString:@"yyyy-MM-dd"];
            [weakSelf.tableV reloadData];
        }];
        [view showOnView:self.view];
    }else if (indexPath.row == 3){
        NSArray *arr = @[@"大专",@"本科",@"硕士",@"博士",@"其他"];
        RWBChouseView *view = [[RWBChouseView alloc]initWithTitles:arr];
        view.chouseTitleblock = ^(NSString *education){
            weakSelf.education = [NSString stringWithFormat:@"%ld",[arr indexOfObject:education]];
            [_tableV reloadData];
        };
        [view showOnView:self.view];
    }
}

-(void)onTapRightBarButton{//保存
    if (_schoolName.length == 0) {
        [TomamaUI creatAlertWithTarget:self message:@"请填写学校名称" timeInterval:2];
        return;
    }
    if (_major.length == 0) {
        [TomamaUI creatAlertWithTarget:self message:@"请填写专业" timeInterval:2];
        return;
    }
    if (_graduationTime.length == 0) {
        [TomamaUI creatAlertWithTarget:self message:@"请选择毕业年份" timeInterval:2];
        return;
    }
    if (_education.length == 0) {
        [TomamaUI creatAlertWithTarget:self message:@"请选择学历" timeInterval:2];
        return;
    }
    [HudHelper showProgressHudWithMessage:@"" toView:self.view];
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:@{@"schoolName":_schoolName,@"major":_major,@"graduationTime":_graduationTime,@"education":_education}];
    NSString *url = @"/user/addLearningExperience";
    if (_edu) {
        [dic setObject:_edu[@"id"] forKey:@"id"];
        url = @"/user/updateLearningExperience";
    }else{
        [dic setObject:[Storage getToken] forKey:@"resumeId"];
    }
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [HttpRequest PostrequestUrl:url Dic:dic completion:^(NSDictionary *responseDic) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [HudHelper hideHudToView:self.view];
                if ([responseDic[@"success"] boolValue]) {
                    [TomamaUI creatAlertWithTarget:self message:_edu?@"修改成功":@"添加成功" timeInterval:2 block:^{
                        if (_block) {
                            _block(responseDic[@"data"]);
                        }
                        [self.navigationController popViewControllerAnimated:YES];
                    }];
                }else{
                    [TomamaUI creatAlertWithTarget:self message:responseDic[@"msg"] timeInterval:2];
                }
            });
        }];
    });
}

@end
